package com.wyz.note.project.auth.core.config.security;

import com.alibaba.fastjson.JSON;
import com.wyz.note.project.auth.common.enums.ResultCode;
import com.wyz.note.project.auth.core.base.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 授权异常
 * @Author: wei yz
 * @Date: 2022/6/20 15:35
 */
public class NoteAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setStatus(ResultCode.PARAMETER_ERROR.getCode());
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(
                JSON.toJSONString(new Result()
                        .setSuccess(false)
                        .setCode(ResultCode.PARAMETER_ERROR)
                        .setMessage("权限异常：" + e.getMessage())));
    }


}
